
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace DemoApp
{
internal class TimedHostedService:IHostedService,IDisposable{
    private readonly ILogger _logger;
    private Timer _timer;

    public TimedHostedService(ILogger<TimedHostedService> logger)
    {
        _logger=logger;
    }

    public Task StartAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("Timed Background Service is Starting.");
        _timer=new Timer(DoWork,null,TimeSpan.Zero,TimeSpan.FromSeconds(5));
        return Task.CompletedTask;
    }

    private void DoWork(object state){
        _logger.LogInformation(string.Format("[{0:yyyy-MM-dd hh:mm:ss}]Timed Background Service is working.", DateTime.Now));
    }

    public Task StopAsync(CancellationToken cancellationToken){
        _logger.LogInformation("Timed Background Service is stopping.");
        _timer?.Change(Timeout.Infinite,0);
        return Task.CompletedTask;
    }

    public void Dispose(){
        _timer?.Dispose();
    }

}
}